var common = require('../../utils/common.js');
const app = getApp()
Page({
  data: {
    shareimg1:'',
    imgShow: false,
    imgbgsrc: '',
    avatar: '',
    loading: false
  },
  onLoad: function (options) {
    this.getPicInfo()
  },
  onReady: function () {
    
  },
  getPicInfo(){
    wx.showLoading({
      title: '加载中',
    })
    const that = this
    var imgbgsrc = 'https://t.image.mrjx.51daydayup.cn/static/drawable-xxhdpi/2020-03-28-invite-share.jpg'
    wx.getImageInfo({
      src: imgbgsrc,
      success: function (res) {
        that.setData({
          imgbgsrc: res.path
        })
        console.log(that.data.imgbgsrc)
      }
    })
    wx.getImageInfo({
      src: app.globalData.userInfo.avatar,
      success: function (res) {
        that.setData({
          avatar: res.path
        })
        console.log(that.data.avatar)
      },
      fail(res){
        console.log(res)
      }
    })
    let promise = new Promise((resolve, reject) => {
      let timer = setInterval(() => {
        // console.log(app.globalData.sessionId,app.globalData.userId)
        if (that.data.avatar !== '' && that.data.imgbgsrc !== '') {
          clearInterval(timer);
          resolve(that.data);
          that.setData({
            loading: true,
          })
          console.log(that.data.logined)
          wx.hideLoading()
        } else {
          
        }
      }, 250);
    });
  },
  getSharePic() {
    const that = this
    wx.showLoading({
      title: '生成中...',
      mask: true
    })
    function sharepic(data, link) {
      console.log(data)
      function myCanvaspic() {
        console.log('star')
        wx.getImageInfo({
          src: link,
          success: function (res) {
            var codeLink = res.path
            var str1 = app.globalData.userInfo.nickname
            const sharepic = wx.createCanvasContext('myCanvas')
            console.log(that.data.imgbgsrc)
            sharepic.drawImage(that.data.imgbgsrc, 0, 0, 1080, 1920)
            sharepic.setFontSize(46)
            // sharepic.setTextAlign('center')
            sharepic.setFillStyle('#333')
            sharepic.fillText(str1, 280, 1694)
            // sharepic.drawImage(avatarSrc, 57, 1650, 186, 186)
            console.log(codeLink)
            sharepic.drawImage(codeLink, 797, 1607, 220, 220)
            sharepic.save();
            sharepic.arc(186 / 2 + 57, 186 / 2 + 1650, 186 / 2, 0, Math.PI * 2, false);
            sharepic.clip(); //画了圆 再剪切  原始画布中剪切任意形状和尺寸。一旦剪切了某个区域，则所有之后的绘图都会被限制在被剪切的区域内
            console.log(that.data.avatar)
            sharepic.drawImage(that.data.avatar, 57, 1650, 186, 186); // 推进去图片
            sharepic.restore(); //恢复之前保存的绘图上下文状态 可以继续绘制
            sharepic.draw(false,function (e) {
              console.log(e)
              wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: 1080,
                height: 1920,
                destWidth: 1080,
                destHeight: 1920,
                canvasId: 'myCanvas',
                success: function (res) {
                  console.log(res.tempFilePath)
                  that.setData({
                    shareimg1: res.tempFilePath,
                    imgShow: true
                  });
                  wx.hideLoading()
                },
                fail(res){
                  console.log(res)
                }
              })
            })
          }
        })
      }
      myCanvaspic()
    }
    var scene = 'userId=' + app.globalData.userId
    console.log(scene)
    common.post({
      url: '/wxa/qrcode/getUnlimitQrcode', 
      data: { 
        scene: scene,
        page: 'pages/index/index',
        sessionId: app.globalData.sessionId,
        width: '220',
        auto_color: 'false',
        line_color: '{"r":"0","g":"0","b":"0"}',
        is_hyaline: 'false'
      }
    })
      .then((res) => {
        console.log(res.data)
        if (res.data.code == "0") {
          console.log(res.data.qrcodeLink)
          sharepic(res.data, res.data.qrcodeLink)
        }
      }).catch((res) => {
        console.log(res)
        app.timeOutTip()
      })
  },
  saveImg() {
    var that = this
    wx.saveImageToPhotosAlbum({
      filePath: this.data.shareimg1,
      success(res) {
        wx.showToast({
          title: '分享图已经存到您的相册',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  close(){
    this.setData({
      imgShow: false
    })
  }
})